package org.dromara.system.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.system.domain.WbMinuteBidBag;
import org.springframework.stereotype.Service;
import org.dromara.system.domain.bo.WbItemFileBo;
import org.dromara.system.domain.vo.WbItemFileVo;
import org.dromara.system.domain.WbItemFile;
import org.dromara.system.mapper.WbItemFileMapper;
import org.dromara.system.service.IWbItemFileService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 项目附件Service业务层处理
 *
 * @author Lion Li
 * @date 2024-03-22
 */
@RequiredArgsConstructor
@Service
public class WbItemFileServiceImpl implements IWbItemFileService {

    private final WbItemFileMapper baseMapper;

    /**
     * 查询项目附件
     */
    @Override
    public WbItemFileVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询项目附件列表
     */
    @Override
    public TableDataInfo<WbItemFileVo> queryPageList(WbItemFileBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WbItemFile> lqw = buildQueryWrapper(bo);
        lqw.orderByDesc(WbItemFile::getOssId);
        Page<WbItemFileVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询项目附件列表
     */
    @Override
    public List<WbItemFileVo> queryList(WbItemFileBo bo) {
        LambdaQueryWrapper<WbItemFile> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<WbItemFile> buildQueryWrapper(WbItemFileBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WbItemFile> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getItemId() != null, WbItemFile::getItemId, bo.getItemId());
        lqw.eq(bo.getCompanyId()!=null,WbItemFile::getCompanyId,bo.getCompanyId());
        lqw.eq(bo.getProductId()!=null,WbItemFile::getProductId,bo.getProductId());
        lqw.like(StringUtils.isNotBlank(bo.getFileName()), WbItemFile::getFileName, bo.getFileName());
        lqw.eq(bo.getFileType() != null, WbItemFile::getFileType, bo.getFileType());
        lqw.eq(StringUtils.isNotBlank(bo.getUrl()), WbItemFile::getUrl, bo.getUrl());
        lqw.eq(bo.getFileSize() != null, WbItemFile::getFileSize, bo.getFileSize());
        return lqw;
    }

    /**
     * 新增项目附件
     */
    @Override
    public Boolean insertByBo(WbItemFileBo bo) {
        WbItemFile add = MapstructUtils.convert(bo, WbItemFile.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setOssId(add.getOssId());
        }
        return flag;
    }

    /**
     * 修改项目附件
     */
    @Override
    public Boolean updateByBo(WbItemFileBo bo) {
        WbItemFile update = MapstructUtils.convert(bo, WbItemFile.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(WbItemFile entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除项目附件
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public Boolean deleteByItemId(String itemId) {
        return baseMapper.delete(new LambdaQueryWrapper<WbItemFile>().eq(WbItemFile::getItemId,itemId)) > 0;
    }
}
